
from sanic import Sanic
from sanic import response
from sanic.log import logger
from sanic_cors import CORS
from db_interface import ConnPG



from db_interface.lately_project import LatelyProject

app = Sanic(__name__)
CORS(app)



@app.listener('after_server_start')
async def start_db_pool(app, loop):
    ConnPG(loop=loop)
    logger.debug('Server PostgreSQL successfully started!')

@app.route("/")
async def test(request):
    return response.json({"hello": "world"})



@app.route("/lately_odd")#不传参数默认最近一个月来往的定单
async def lately_odd(request):
    start_date = request.args.get('start_date')
    end_date = request.args.get('end_date')
    result = await LatelyProject().lately_odd(start_date, end_date)
    # print(result)
    return response.json(result)


@app.route("/lately_subject")#一个月来往的商户
async def lately_subject(request):

    result = await LatelyProject().analyse_subject()
    # print(result)
    return response.json(result)

@app.route("/lately_brand")#所有brand默认最近100个的新添加SKU的品牌靠前
async def lately_brand(request):

    result = await LatelyProject().analyse_bran()
    # print(result)
    return response.json(result)

@app.route("/get_sku_total") # 获取所有sku剩余量
async def get_sku_total(request):

    result = await LatelyProject().get_sku_total()
    # print(result)
    return response.json(result)

@app.route("/get_sku_total_by_warehouse_id") # 获取所有sku剩余量
async def get_sku_total_by_warehouse_id(request):

    print(request.args)

    warehouse_id = request.args.get("warehouse_id")

    result = await LatelyProject().get_sku_total_by_warehouse_id(warehouse_id)
    # print(result)
    return response.json(result)

if __name__ == "__main__":

    app.run(host="0.0.0.0", port=8000, debug=True)


